package com.tiandang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tiandang.core.model.*;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/wechat/home")
public class HomeController {

  @Autowired
  private PromoService promoService;

  @Autowired
  private ManagerService managerService;

  @Autowired
  private PromoManagerService promoManagerService;

  @Autowired
  private PromoProductService promoProductService;


  @GetMapping("/getPromoList")
  public CommonReturnType getPromoList(@RequestParam(value = "managerId") Integer managerId) {

    //获取团购信息
    //先从PromoManager拿到此managerId所有开团的记录promoIdList,再从promo表查
//    List<PromoManager> promoManagerList = promoManagerService.getListByManagerId(managerId);
//    if(promoManagerList == null || promoManagerList.size() == 0){
//      return CommonReturnType.error(-1,"团长没有开团记录");
//    }
//
//    List<Integer> promoIdList = new ArrayList<>();
//    for(PromoManager promoManager : promoManagerList){
//      promoIdList.add(promoManager.getPromoId());
//    }
//
//    List<Promo> promoList = promoService.getListByIdList(promoIdList);



    //直接用managerId获取promoList
    List<Promo> promoList = promoService.getListByManagerId(managerId);
    if(promoList == null || promoList.size() == 0){
      return CommonReturnType.error(-1,"团长没有开团记录");
    }

    List<Integer> promoIdList = new ArrayList<>();
    for(Promo promo :promoList){
      promoIdList.add(promo.getId());
    }

    Long now = new Date().getTime();
    promoList = promoList.stream().map(promo -> {
      if (promo.getStartTimestamp() > now) {
        promo.setStatus(0); //0未开始
      } else if (promo.getEndTimestamp() < now) {
        promo.setStatus(2); //2已结束
      } else {
        promo.setStatus(1); //1进行中
      }
      return promo;
    }).collect(Collectors.toList());

    //获取团长信息
    Manager manager = managerService.getManagerById(managerId);
    if(manager == null){
      return CommonReturnType.error(-1,"团长不存在");
    }

    //获取9个商品
    List<PromoProduct> productList = promoProductService.getListByPromoIdList(promoIdList);
    if(productList == null || productList.size() == 0){
      return CommonReturnType.error(-1,"尚未有商品上架");
    }

    for(Promo promo : promoList){
      List<PromoProduct> promoProductList = new ArrayList<>();
      for(PromoProduct promoProduct : productList){
        if(promo.getId().intValue() == promoProduct.getPromoId().intValue()){
          if(promoProductList.size() < 10){
            promoProductList.add(promoProduct);
          }
        }
      }
      promo.setPromoProductList(promoProductList);
    }

    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("systemTime", new Date().getTime());
    resultMap.put("promoList", promoList);
    resultMap.put("manager", manager);
    return CommonReturnType.success(resultMap);
  }

}
